<?php
require_once 'mods/user.php';

class SdpPerformerHelper{
	public static $instance=null;
	var $performer=null;
	var $_allowed=null;
	public static function inst(){
		if(null==self::$instance){
			self::$instance=new SdpPerformerHelper();
		}
		return self::$instance;
	}

	public static function getAllowedPerformers(){
		global $_user;
		$allowed_performers=array();
		if ( is_object($_user) ) {
			$group=$_user->getGroup();
			if (($_user->group != $group->admin_group)
				&& ($_user->group != $group->poweruser_group)
				&& ($_user->group != $group->project_manager_group)
				&& ($_user->group != $group->vbr_manager_group)) {
				
				$allowed_performers[]=$_user->human;
				$show_teams = ($_user->group == $group->team_leader_group);
				
				if ($_user->group == $group->team_leader_group) {
					$team=$_user->getHuman()->getTeam();
					foreach ($team->getHumans() as $user_human) {
						$allowed_performers[]=$user_human->id;
					}
				}
			}
		}
		return $allowed_performers;
	}

	public static function getAllowedPerformer($performer, $allowed_performers){
		global $_user;
		if (!empty($allowed_performers)) {
			if ($performer > 0) {
				if (!in_array($performer, $allowed_performers)) {
					$performer=$_user->getHuman()->id;
				}
			} else {
				$team=new Team();
				$team->load(abs($performer));	
				if ($team->getTeamLeader()->id != $_user->getHuman()->id) {
					$performer=$_user->getHuman()->id;
				}
			}
		}
		return $performer;
	}

	public function load($aPerformer){
		$this->performer=$aPerformer;
		$this->_allowed=null;
		$allowed_performers=self::getAllowedPerformers();
		if (!empty($allowed_performers)){
			$this->_allowed=array();
			foreach ($allowed_performers as $value){
				$this->_allowed[$value]=true;
			}
		}
	}
	public function AllowedPerformer($humanId){
		global $_user;
		if ( $_user ) {
			if ($this->performer != $_user->getHuman()->id){
				$this->load($_user->getHuman()->id);
			}
			return ((null===$this->_allowed)||isset($this->_allowed[$humanId]));
		}
		return false;
	}
}
?>